/*-//GEN-BEGIN:copyright
 * ForBILE - Framework for Building Interactive Learning Environments
 * Computing Institute - Federal University of Alagoas - Brazil
 * Group of Otimization of the Web
 *
 * Copyright (C) 2007 Ig Bittencourt (ig.ibert@gmail.com)
 *		      Camila Nunes (camilapbnunes@hotmail.com)
 *	              Marcos Tadeu (zero.ufal@gmail.com)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *///GEN-END:copyright
package br.ufal.ic.forbile.agents.mediator.behaviours;

import br.ufal.ic.forbile.agents.ForBILEAgent;
import br.ufal.ic.forbile.agents.behaviours.GeneralActuator;
import br.ufal.ic.forbile.agents.behaviours.GeneralSensor;
import br.ufal.ic.forbile.agents.mediator.MediatorAgent;
import br.ufal.ic.forbile.log.FacadeLog;
import br.ufal.ic.forbile.log.PersistenceOWLLog;
import br.ufal.ic.utils.Constants;
import br.ufal.ic.utils.RegionServer;
import br.ufal.ic.utils.SharedMem;
import jade.core.AID;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import java.rmi.Naming;
import java.util.logging.Logger;

/**
 * SensorMediator1.java
 *
 * <p>A full description of this class.
 *
 * @see SomeRelatedClass.
 *
 * @author <a href="mailto:ig.ibert@gmail.com">ig</a>.
 * @since 0.1
 * @version 0.1
 *
 */
public class SensorMediator extends GeneralSensor {
    
    // Constants -----------------------------------------------------
    
    // Attributes ----------------------------------------------------
    
    // Static --------------------------------------------------------
    
    private static Logger logger;
    
    static {
        logger = Logger.getLogger(SensorMediator.class.getName());
    }
    
    // Constructors --------------------------------------------------
    
    /**
     * Creates a new instance of SensorMediator1
     */
    public SensorMediator(final ForBILEAgent agent, final long time) {
        super(agent, time);
    }
    
    // Public --------------------------------------------------------
    
    // GeneralSensor implementation ----------------------------------
    
    protected void callBehaviour(final ACLMessage message) {
        
        if (message.getInReplyTo() != null) {
            
            if ( (message.getInReplyTo().equalsIgnoreCase("RETRIEVE")) && !(message.getProtocol() != null) ) {
                AID aid = new AID("ControllerAgent",AID.ISLOCALNAME);
                myAgent.addBehaviour(new GeneralActuator(aid, null, "O2ACommunication"));
            } else {
                if (message.getInReplyTo().equalsIgnoreCase("RETRIEVE")) {
                    ((MediatorAgent) myAgent).configureSATAProtocols();
                }
            }
            
            
            if (message.getInReplyTo().equalsIgnoreCase("CREATE")) {
                PersistenceOWLLog persistenceOWLLog = new PersistenceOWLLog();
                try {
                    SharedMem mem = (SharedMem) Naming.lookup(RegionServer.HOST_URL);
                    persistenceOWLLog = (PersistenceOWLLog) mem.getRegion(Constants.OWLPersistence).retrieveIfExist(persistenceOWLLog);
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                String individual = (String) persistenceOWLLog.getConfiguration().get("habilityName");
                logger.info("Data Created to ATA Configuration" + persistenceOWLLog.getConfiguration());
                logger.info("individual: " + individual);
                ((MediatorAgent) myAgent).ATAAgentConfigured(individual, true);
            }
        }
        
        if (message.getProtocol() != null) {
            if (message.getProtocol().equalsIgnoreCase("CreateSATA")) {
                ( (MediatorAgent) myAgent).requestATAAgents();
            }
            
            if (message.getProtocol().equalsIgnoreCase("application")) {
                FacadeLog facadeLog = new FacadeLog();
                
                try {
                    facadeLog = (FacadeLog) message.getContentObject();
                    
                } catch (UnreadableException ex) {
                    ex.printStackTrace();
                    
                }
            }
        }
        try {
            
            if ( (message.getContentObject() != null) && !(message.getInReplyTo() != null ) ) {
                FacadeLog facadeLog = new FacadeLog();
                facadeLog = (FacadeLog) message.getContentObject();
                AID[] aid = ( (MediatorAgent) myAgent).searchAgent(facadeLog.getService(), facadeLog.getAbility());
                System.out.println(aid[0]);
                myAgent.addBehaviour(new GeneralActuator(aid[0], facadeLog.getMessage()));
            }
            
        } catch (UnreadableException ex) {
            ex.printStackTrace();
        }
        
    }
    
    // Y overrides ---------------------------------------------------
    
    // Package protected ---------------------------------------------
    
    // Protected -----------------------------------------------------
    
    // Private -------------------------------------------------------
    
    // Inner classes -------------------------------------------------
    
}
